package ar.com.ford.it.fullEconomics.persistence.dao.query;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.GregorianCalendar;
import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp01Part;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp06Mix;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class MixFindQuery implements CustomExpressionFactory {

    /**
     * Busca los Mix segun un mix dado y una parte estas partes pueden o no estar completas en su totalidad
     */
    
    public Expression createExpression(ExpressionBuilder builder, Object[] parameters) {
        Expression exp = null;
        Teesp06Mix mix = (Teesp06Mix) parameters[0];
        Teesp01Part part = mix.getTeesp01Part();
        String prefix = part.getEesp01PartPrefixCodeC();
        String base = part.getEesp01PartBaseCodeC();
        String suffix = part.getEesp01PartSuffixCodeC();
        String partClass = part.getEesp01PartClassCodeC();
        Teesg01Plant plant = mix.getEesp06PlantCodeC();
        exp = builder.get("eesp06PlantCodeC").equal(plant);

        if( partClass != null && !partClass.equalsIgnoreCase("-1"))
            exp = exp.and(builder.get("teesp01Part").get("eesp01PartClassCodeC").equal(partClass));
        if (prefix != null) {
            prefix= prefix.trim();
            prefix += "%";
            exp = exp.and(builder.get("teesp01Part").get("eesp01PartPrefixCodeC").toChar().like(prefix));
        }
        if (base != null) {
            base= base.trim();
            base += "%";
            exp = exp.and(builder.get("teesp01Part").get("eesp01PartBaseCodeC").toChar().like(base));
        }
        if (suffix != null) {
            suffix = suffix.trim();
            suffix += "%";
            exp = exp.and(builder.get("teesp01Part").get("eesp01PartSuffixCodeC").toChar().like(suffix));
        }
        
        if(mix.getEesp06MixEffectiveDateY()!=null){
            exp = exp.and(builder.get("eesp06MixEffectiveDateY").equal(mix.getEesp06MixEffectiveDateY()));
            
        }
        if (mix.getEesp06VehicleCodeC()!=null) {
            exp = exp.and(builder.get("eesp06VehicleCodeC").get("cas012VehicleCodeC").equal(mix.getEesp06VehicleCodeC().getCas012VehicleCodeC()));
        }
        return exp;
    }
    
    public static Timestamp sumaDias(Timestamp fch, int dias) {
        Calendar cal = new GregorianCalendar();
        cal.setTimeInMillis(fch.getTime());
        cal.add(Calendar.DATE, dias);
        return (Timestamp) new Timestamp(cal.getTimeInMillis());
    }

    public static Timestamp sumaMes(Timestamp fch, int meses) {
        Calendar cal = new GregorianCalendar();
        cal.setTimeInMillis(fch.getTime());
        cal.add(Calendar.MONTH, meses);
        return (Timestamp) new Timestamp(cal.getTimeInMillis());
    }

}
